package com.abe.bathsystem.contract.presenter;

import com.abe.bathsystem.contract.CommonContract;
import com.abe.bathsystem.contract.CommonModel;
import com.abe.bathsystem.entity.ReqBaseEntity;
import com.abe.bathsystem.entity.ReqBaseNoDataEntity;
import com.abe.bathsystem.entity.ReqConstant;
import com.abe.bathsystem.entity.http.HttpBathPosStateEntity;
import com.abe.bathsystem.entity.http.HttpSensorEntity;
import com.abe.mvpcore.BasePresenter;
import com.abe.mvpcore.OnResultListener;

import java.util.List;

public class BathPosPresenter extends BasePresenter<CommonContract.BathPosView, CommonModel> implements CommonContract.BathPosPresenter {

    @Override
    public void updateSensor(List<HttpSensorEntity> sensorEntities) {
        getModel().uploadSensorData(sensorEntities, new OnResultListener<ReqBaseNoDataEntity>() {
            @Override
            public void onStart() {

            }

            @Override
            public void onEnd() {
                BathPosPresenter.this.onEnd(BathPosPresenter.class.getSimpleName() + "-updateSensor");
            }

            @Override
            public void onSuccess(ReqBaseNoDataEntity data) {
                if (BathPosPresenter.this.onSuccess(data)) {
                    getView().onUpdateSensor(data.getCode() == ReqConstant.NET_RESULT_OK);
                }
            }

            @Override
            public void onFailure(String message) {
                BathPosPresenter.this.onFailure(BathPosPresenter.class.getSimpleName() + "-updateSensor");
            }
        });
    }

    @Override
    public void updateBathPosState(final int position, final int status, int sensor01, int sensor02) {
        getModel().updateBathPosState(position, status, sensor01, sensor02, new OnResultListener<ReqBaseNoDataEntity>() {
            @Override
            public void onStart() {

            }

            @Override
            public void onEnd() {
                BathPosPresenter.this.onEnd(BathPosPresenter.class.getSimpleName() + "-updateBathPosState");
            }

            @Override
            public void onSuccess(ReqBaseNoDataEntity data) {
                if (BathPosPresenter.this.onSuccess(data)) {
                    getView().onUpdateBathPos(position, status, data.getCode() == ReqConstant.NET_RESULT_OK);
                }
            }

            @Override
            public void onFailure(String message) {
                BathPosPresenter.this.onFailure(BathPosPresenter.class.getSimpleName() + "-updateBathPosState");
            }
        });
    }

    @Override
    public void getRoomStatus(final boolean isChoosePos) {
        getModel().getRoomStatus(new OnResultListener<ReqBaseEntity<List<HttpBathPosStateEntity>>>() {
            @Override
            public void onStart() {

            }


            @Override
            public void onEnd() {
                BathPosPresenter.this.onEnd(BathPosPresenter.class.getSimpleName() + "-getRoomStatus");
            }

            @Override
            public void onSuccess(ReqBaseEntity<List<HttpBathPosStateEntity>> data) {
                if (BathPosPresenter.this.onSuccess(data) && data.getCode() == ReqConstant.NET_RESULT_OK) {
                    getView().onGetBathPosState(data.getData(), isChoosePos);
                }
            }

            @Override
            public void onFailure(String message) {
                BathPosPresenter.this.onFailure(BathPosPresenter.class.getSimpleName() + "-getRoomStatus");
            }
        });
    }
}
